﻿using System;
using GameEngine.Model;
using GameEngine.Model.Data;
using GameEngine.View;
using GameEngine.View.Components;
using PureMVC.Interfaces;
using PureMVC.Patterns;

namespace GameEngine.Controller
{
    public class CreateGameCommand<MyIdType, MyBaseUIElementType>:SimpleCommand
        where MyIdType : IComparable<MyIdType>
    {
        override public void Execute(INotification notification)
        {
            var gameVO = notification.Body as IGameVO<MyIdType>;
            /*foreach (KeyValuePair<int, ILevelVO<ThingVOId>> level in gameVO.Levels)
            {
                foreach (IWorldVO<ThingVOId> world in level.Value.Worlds)
                {
                    IMapDataProxy mapDataProxy = CreateMapDataProxy(world.Map, gameVO.TileSize, gameVO.MaxHeight);
                    ApplicationFacade.Instance.RegisterProxy(mapDataProxy);
                    IWorldDataProxy<ThingVOId> worldDataProxy = CreateWorldDataProxy(world);
                    ApplicationFacade.Instance.RegisterProxy(worldDataProxy);
                }
                ApplicationFacade.Instance.RegisterProxy(CreateLevelDataProxy(level.Value));
            }*/

            var gameDataProxy = ModelFacade<MyIdType>.Instance.CreateGameDataProxy(gameVO);
            ApplicationFacade.Instance.RegisterProxy(gameDataProxy);
            
            ModelFacade<MyIdType>.Instance.Engine.Game = gameDataProxy;

            var gameMed = ViewFacade<MyIdType, MyBaseUIElementType>.Instance.CreateGameMediator(
                    gameDataProxy,
                    ViewFacade<MyIdType, MyBaseUIElementType>.Instance.CreateGameView());

            //ViewFacade<MyGameView, MyLevelView, MyWorldView, MyMapView, MyThingView, MyIdType, MyBaseUIElementType>.Helper.GetLevelMediator(gameVO.CurrentLevel),
            ApplicationFacade.Instance.RegisterMediator(gameMed);

            
            foreach (IThingTypeVO<MyIdType> thingType in gameVO.ThingTypes)
            {
                ApplicationFacade.Instance.RegisterProxy(ModelFacade<MyIdType>.Instance.CreateThingTypeDataProxy(thingType));
            }
        }
    }
}
